<?php
require_once("include/config.php");

MySQL::connect();
/**
$sql = "RENAME TABLE `reader`.`reader_discussion`  TO `reader`.`old_reader_discussion`";
MySQL::query($sql,__FILE__,__LINE__);

Discussion::install();

Competition::install();

$sql = "ALTER TABLE ".TagReference::getTableName()." ADD type ENUM( 'book', 'competition', 'writing' ) NOT NULL DEFAULT 'book'";
MySQL::query($sql,__FILE__,__LINE__);
$sql = "ALTER TABLE ".TagReference::getTableName()." CHANGE `book` `target` INT( 25 ) NULL DEFAULT NULL";  
MySQL::query($sql,__FILE__,__LINE__);
*/
$sql = "SELECT * FROM ".Comment::getTableName();
$res = MySQL::query($sql,__FILE__,__LINE__);
while($rec = mysql_fetch_object($res)) {
	Book::getInfo($rec->book);
	MySQL::insert(Discussion::getTableName(),array(
		"text" => $rec->text,
		"user" => $rec->user,
		"date" => $rec->date,
		"follow" => $rec->book,
		"type"	=> "book"
	));
}
$sql = "SELECT * FROM ".WritingComment::getTableName();
$res = MySQL::query($sql,__FILE__,__LINE__);
while($rec = mysql_fetch_object($res)) {
	MySQL::insert(Discussion::getTableName(),array(
		"text" => $rec->text,
		"user" => $rec->user,
		"date" => $rec->date,
		"title" => $rec->title,
		"follow" => $rec->writing,
		"parent" => $rec->parent,
		"type"	=> "writing"
	));
}
$sql = "SELECT * FROM old_reader_discussion WHERE follow = 0";
$res = MySQL::query($sql,__FILE__,__LINE__);
while($rec = mysql_fetch_object($res)) {
	$new = MySQL::insert(Discussion::getTableName(),array(
		"text" => $rec->text,
		"user" => $rec->user,
		"date" => $rec->date,
		"title" => $rec->title,
		"follow" => $rec->topic,
		"parent" => 0,
		"type"	=> "topic"
	));
	$sql = "SELECT * FROM old_reader_discussion WHERE follow = $rec->id";
	$res2 = MySQL::query($sql,__FILE__,__LINE__);
	while ($help = mysql_fetch_object($res2)) {
		MySQL::insert(Discussion::getTableName(),array(
			"text" => $help->text,
			"user" => $help->user,
			"date" => $help->date,
			"title" => $help->title,
			"follow" => $new,
			"parent" => 0,
			"type"	=> "discussion"
		));
	}
}
$page = new CommonPage();
$page->view();
?>